/**
  ******************************************************************************
  * @file    MCX314.c
  * @author  Nguyen Thinh
  * @version V1.0.0
  * @date    23/08/2014
  * @brief   Library Function Driver for MCX314
  * 
  ******************************************************************************
  * @attention
  ******************************************************************************
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MCX314_H
#define __MCX314_H

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
#define Stt_Read  1
#define Stt_Write  0
// ----- MCX314As/AL register address definition -------------------------------
#define   Reg0    0x0  //Command register 
#define   Reg1    0x1  //Mode register 1 
#define   Reg2    0x2  //Mode register 2 
#define   Reg3    0x3  //Mode register 3 
#define   Reg4    0x4  //Output register 
#define   Reg5    0x5  //Interpolation mode register 
#define   Reg6    0x6   //Low word bits data writing register 
#define   Reg7    0x7   //High word bits data writing register 
// ----- Axis ------------------------------------------------------------------
#define   Axis_X  0x01
#define   Axis_Y  0x02
#define   Axis_Z  0x04
#define   Axis_U  0x08
// ----- Command list ----------------------------------------------------------
/*------ Write command----------------*/
#define   R       0x00  //Range setting: 8,000,000(multiple=1) ~ 16,000(multiple=500)
#define   K       0x01  //Jerk setting  (Acceleration increasing rate): 1 ~ 65,535   
#define   A       0x02  //Acceleration setting  A  1 ~ 8,000 
#define   D       0x03  //Deceleration setting  D  1 ~ 8,000
#define   SV      0x04  //Initial speed setting  SV  1 ~ 8,000 
#define   V       0x05  //Drive speed setting  V  1 ~ 8,000
#define   P       0x06  //Output pulse numbers / finish point:
                        //Output pulse numbers: 0~268,435,455 
										    //finish point: -2,147,483,646 ~ +2,147,483,646
#define   DP      0x07  //Manual deceleration point setting  DP  0 ~ 4,294,967,295
#define   C       0x08  //Circular center point setting: -2,147,483,646 ~ +2,147,483,646
#define   LP_S    0x09  //Logical position counter setting: -2,147,483,648 ~ +2,147,483,647
#define   EP_S    0x0A  //Real position counter setting: -2,147,483,648 ~ +2,147,483,647
#define   CP      0x0B  //COMP + register setting  CP  -2,147,483,648 ~ +2,147,483,647
#define   CM      0x0C  //COMP -register setting   CM  -2,147,483,648 ~ +2,147,483,647
#define   AO      0x0D  //Acceleration counter offsetting  AO  -32,768 ~ +32,767
#define   L       0x0E  //Deceleration increasing rate setting L  1 ~ 65,535 
#define   EM      0x60  //Extension mode setting  EM  (Bit data)
#define   HV      0x61  //Home detection speed setting  HV  1 ~ 8,000
#define   SM      0x64  //Synchronous action mode setting  SM  (Bit data)

/*------ Read command----------------*/
#define   LP_R    0x10  //Logical position counter reading  LP  -2,147,483,648 ~ +2,147,483,647
#define   EP_R    0x11  //Real position counter reading  EP  -2,147,483,648 ~ +2,147,483,647
#define   CV      0x12  //Current drive speed reading  CV  1 ~ 8,000
#define   CA      0x13  //Acceleration / deceleration reading  CA  1 ~ 8,000
#define   SB      0x14  //Synchronous buffer register reading SB  -2,147,483,648 ~ +2,147,483,647

/*------ Driving command----------------*/
#define   Dir_Fix_Dri_P   0x20  //+ direction fixed pulse driving
#define   Dir_Fix_Dri_M   0x21  //- direction fixed pulse driving
#define   Dir_Con_Dri_P   0x22  //+ direction continuous driving
#define   Dir_Con_Dri_M   0x23  //- direction continuous driving
#define   Dri_Sta_Hol     0x24  //Drive start holding
#define   Dri_Hol_Rele    0x25  //Drive start holding release / stop status clear
#define   Dec_Sto         0x26  //Decelerating stop
#define   Sud_Sto         0x27  //Sudden stop

/*------ Other command----------------*/
#define   NOP             0x0f    //NOP (for axis switching)
#define   RESET           0x8000  // Software Reset MCX314
/* Exported macro ------------------------------------------------------------*/
#define   BUSY            GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8)

#define   DIR_OUT         GPIO_SetBits(GPIOD, GPIO_Pin_7)
#define   DIR_IN          GPIO_ResetBits(GPIOD, GPIO_Pin_7)

#define   WRN_Active      GPIO_ResetBits(GPIOD, GPIO_Pin_2)
#define   WRN_Deactive    GPIO_SetBits(GPIOD, GPIO_Pin_2)

#define   RDN_Active      GPIO_ResetBits(GPIOD, GPIO_Pin_1)
#define   RDN_Deactive    GPIO_SetBits(GPIOD, GPIO_Pin_1)

#define   CSN_Active      GPIO_ResetBits(GPIOD, GPIO_Pin_3)
#define   CSN_Deactive    GPIO_SetBits(GPIOD, GPIO_Pin_3)

/* Exported functions --------------------------------------------------------*/ 

/* Initialization and Configuration functions *********************************/
void MCX314_Init(void);
void MCX314_Write(int8_t add, int32_t data);
int32_t MCX314_Read(int8_t add);
void MCX314_Command(int32_t axis, int16_t cmd);
void MCX314_ExtMode(int32_t axis,int32_t em6data,int32_t em7data);

// wreg 1 (axis assignment, data) ----Write register 1 setting
void MCX314_WReg1(int32_t axis,int32_t wdata);
// wreg 2 (axis assignment, data) ----Write register 2 setting
void MCX314_WReg2(int32_t axis,int32_t wdata);
// wreg 3 (axis assignment, data) ----Write register 3 setting
void MCX314_WReg3(int32_t axis,int32_t wdata);

// range(axis assignment, data) -----For range (R) setting
void MCX314_Range(int32_t axis,int32_t wdata);

// acac(axis assignment, data) -----For jerk (K) setting
void MCX314_Acac(int32_t axis,int32_t wdata);
 
// dcac(axis assignment, data) -----For S-curve Deceleration increasing rate (L) setting
void MCX314_Dcac(int32_t axis,int32_t wdata);

// acc(axis assignment, data) -----For acceleration/deceleration (A) setting
void MCX314_Acc(int32_t axis,int32_t wdata);

// dec( axis assignment, data) -----For deceleration (D) setting
void MCX314_Dec(int32_t axis,int32_t wdata);

// startv(axis assignment, data) -----For initial speed (SV) setting
void MCX314_Startv(int32_t axis,int32_t wdata);

// speed(axis assignment, data) -----For drive speed (V) setting
void MCX314_Speed(int32_t axis,int32_t wdata);

// pulse( axis assignment, data) -----For output pulse output/finish point (P) setting
void MCX314_Pulse(int32_t axis,int32_t wdata);

// decp(axis assignment, data) -----For manual deceleration (DP) setting
void MCX314_Decp(int32_t axis,uint32_t wdata);

// lp(axis assignment, data) -----For logical position counter (LP ) setting
void MCX314_LogPos(int32_t axis,int32_t wdata);

// ep(axis assignment, data) -----For real position counter (EP) setting
void MCX314_EncPos(int32_t axis,int32_t wdata);
 
// compp(axis assignment, data) -----For COMP+ (CP) setting
void MCX314_CompP(int32_t axis,int32_t wdata);

// compm(axis assignment, data) -----For COMP - (CM) setting
void MCX314_CompM(int32_t axis,int32_t wdata);

// accofst(axis assignment, data) ----For acceleration counter shift (AO) setting
void MCX314_AccOfst(int32_t axis,int32_t wdata);

// readlp(axis assignment) -----For logical position counter (LP) reading
int32_t MCX314_ReadLogPos(int32_t axis);

// readep(axis assignment) -----For real position counter (EP) reading
int32_t MCX314_ReadEncPos(int32_t axis);

// wait(axis assignment) -----For waiting for drive stop
void MCX314_Wait(int32_t axis);

#endif /* __MCX314_H */

